
/*DWR errorHandle*/
DWREngine.setErrorHandler(errorhandle);
var messagePool = {};
function errorhandle(){
 //alert("Remote server error,Please refresh pager later");
 var messagePannel = document.getElementById("messagePannel");
 var messageContent = new paramInit().getRemoteErrorMsg();
 if(messagePannel){
    if(messagePool['dwrError']==null){
     messagePannel.innerHTML = messagePannel.innerHTML+"<br>"+messageContent;
     messagePool['dwrError']=messageContent;
    }
 }
 clearAllRefreshHandles();
}


/**
if Remote server error ,Should stop all refresh
*/
function clearAllRefreshHandles(){
 for(i=0;i<channel.control.InstanceArray.length;i++){
   var tempHandle = eval(channel.control.InstanceArray[i]+".channelRefreshHandle");
   if(tempHandle){
     clearTimeout(tempHandle);
   }
 }
}

function findElement(divName,id,type){
   var div = document.getElementById(divName);
   if(type!=null){
     var eles=div.getElementsByTagName(type);
     try{
      for(el=0;el<eles.length;el++){
        if(id==eles[el].id||id==eles[el].name){
          return eles[el];
        } 
      }
     }catch(e){
     alert("NO SUCH ELEMENT IN "+divName);
     }
   }else{
   //EMPTY
    return false;
   }
}

function createElement(parent,id,type){
  var node=document.createElement(type);
  node.id=id;
  with(node.style){
    position = "absolute";
	zIndex = 998;
	display = "none";
	backgroundColor="red";
	//width=10px;
	//height=5px;
  }
  parent.appendChild(node);
  return node;
}
//------------------------------------------------------------------------
//--execute event---------------------------------------------------------

var isRefresh = false;
var eventPool = new Array();
var i=0;

setInterval(exeEvent,10000);
function putPool(event){
eventPool.push(event);
}
function exeEvent(){
  var isExe = false;
  
  for(y=0;y<eventPool.length;y++){
    var eventE = eventPool[y];
    if(eventE.element.checked){
      AjaxEventProcessor.execute(eventE.arg,eventE.actionType,eventE.method);
      isExe = true;
    }
  }
  z = eventPool.length;
  while(z>0){
  z--;
  eventPool.remove(z);
  }

  if(isExe){
  //reStartFreshRequenceDataByDivName(eventE.divName);
  eval("channel"+eventE.divName+".refreshChannel()");
  }
  return false;
}

function selectAllCheck(elementName,selectAll){
  var element = document.getElementsByName(elementName);
  var n = element.length;    
  if(selectAll.checked){
    for(var i=0;i<n;i++){
      element[i].checked = true;
    }
  }else{
    for(var i=0;i<n;i++){
  	  element[i].checked = false;
     }
  }
}  

function exeEventRead(elementName,buttonID){
  document.getElementById(buttonID).className = "but-standard-disabled";
  var eventReadIDList = new Array();
  var element = document.getElementsByName(elementName);
  var n = element.length;
  for(var i=0;i<n;i++){
  	if(element[i].checked&&element[i].value!=""){
  	  eventReadIDList.push(element[i].value);
  	}
  }
  AjaxEventProcessor.execute(eventReadIDList,'SL','updateReadActionOperationLog',{callback:function(){eval("channelSL.refreshChannel()");}});
}

function checkButtonClass(buttonID,elementName){
  var button = document.getElementById(buttonID);
  var element = document.getElementsByName(elementName);
  var n = element.length;
  document.getElementById(buttonID).className = "but-standard-disabled";
  for(var i=0;i<n;i++){
    if(element[i].checked&&element[i].value!=""){
 	  document.getElementById(buttonID).className = "but-standard";
 	  eval("channelSL.stop()");
 	  break
 	}
  }
  if("but-standard-disabled"==document.getElementById(buttonID).className){
    eval("channelSL.refreshChannel()");
  }
}


function eventExecute(divName,actionType,method,arg,delayTime,element){
  
  if(!element.checked)
  return false;
  var _event = {"divName":divName,"actionType":actionType,"method":method,"arg":arg,"element":element};
  putPool(_event);
}

Array.prototype.remove=function(dx)
  {
    if(isNaN(dx)||dx>this.length){return false;}
    for(var i=0,n=0;i<this.length;i++)
    {
        if(this[i]!=this[dx])
        {
            this[n++]=this[i]
        }
    }
    this.length-=1
  }
//--------------------------------------
//ATTENTION: THIS IS ADDED as a switch control to determin whether showing DWR Loading alert or not
var ENABLE_USE_LOADINGMESSAGE = false;

function init() {
  if (ENABLE_USE_LOADINGMESSAGE) {
  	DWRUtil.useLoadingMessage();
  }
}

//-------------loading message-------------------------
if (ENABLE_USE_LOADINGMESSAGE) {
	if (window.addEventListener) {
  	window.addEventListener("load", init, false);
	} else if (window.attachEvent) {
  	window.attachEvent("onload", init);
	} else {
  	window.onload = init;
	}
}
